package com.asman.worker;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;

import com.asman.worker.rn.NativeEventUtils;
import com.asman.worker.splash.SplashScreen;
import com.facebook.react.ReactActivity;
import com.facebook.react.bridge.ReactContext;
import com.umeng.analytics.MobclickAgent;

import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;

public class MainActivity extends ReactActivity {

    public static void start(Context context, String json) {
        Intent intent = new Intent(context, MainActivity.class);
        if(!(context instanceof Activity)){
            intent.addFlags(FLAG_ACTIVITY_NEW_TASK);
        }
        intent.putExtra("pushExtraJson", json);
        context.startActivity(intent);
    }

    @Override
    protected String getMainComponentName() {
        return "RNAsmanMaster";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        SplashScreen.show(this);
        super.onCreate(savedInstanceState);
        handlerPushExtra(getIntent());
    }

    @Override
    public void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        handlerPushExtra(intent);
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

//    @Override
//    public void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//    }

    //获取推送传递过来的消息
    public void handlerPushExtra(Intent intent) {
        if (intent != null && intent.hasExtra("pushExtraJson")) {
            String pushExtraJson = intent.getStringExtra("pushExtraJson");
//            Toast.makeText(this, "通知发送：" + pushExtraJson, Toast.LENGTH_SHORT).show();
            new Handler().postDelayed(() -> {
                ReactContext currentReactContext =
                        ((MainApplication) getApplicationContext()).getReactNativeHost().getReactInstanceManager().getCurrentReactContext();
//                Toast.makeText(this, "通知发送" + currentReactContext, Toast.LENGTH_SHORT).show();
                NativeEventUtils.INSTANCE.nativeNotifClick(currentReactContext, pushExtraJson);
            }, 1000);
        }
    }
}
